package com.example.project.controller;

import com.example.project.dto.TcdmSecureHazardListDTO;
import com.example.project.entity.InventoryTrend;
import com.example.project.entity.TcdmSecureHazardList;
import com.example.project.service.TcdmSecureHazardListService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * (TcdmSecureHazardList)表控制层
 *
 * @author makejava
 * @since 2024-02-23 15:18:23
 */
@RestController
@CrossOrigin
@RequestMapping("tcdmSecureHazardList")
public class TcdmSecureHazardListController {
    /**
     * 服务对象
     */
    @Resource
    private TcdmSecureHazardListService tcdmSecureHazardListService;

    /**
     * 分页查询
     *
     * @param tcdmSecureHazardListDTO 筛选条件
     * @return 查询结果
     */
    @CrossOrigin
    @PostMapping("findPage")
    public ResponseEntity<Page<TcdmSecureHazardList>> queryByPage(@RequestBody TcdmSecureHazardListDTO tcdmSecureHazardListDTO) throws ParseException {
        com.example.project.entity.Page page=tcdmSecureHazardListDTO.getPage();
        Date end=null;
        Date star=null;
        if(tcdmSecureHazardListDTO.getMyTime2().getEndTime()!=null&&tcdmSecureHazardListDTO.getMyTime2().getEndTime()!=""){
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            end=format.parse(tcdmSecureHazardListDTO.getMyTime2().getEndTime());
        }
        if(tcdmSecureHazardListDTO.getMyTime2().getStartTime()!=null&&tcdmSecureHazardListDTO.getMyTime2().getStartTime()!=""){
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            star =format.parse(tcdmSecureHazardListDTO.getMyTime2().getStartTime());

        }
        return ResponseEntity.ok(this.tcdmSecureHazardListService.queryByPage(tcdmSecureHazardListDTO.getTcdmSecureHazardList(),PageRequest.of(page.getPage(), page.getSize()),star,end));
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<TcdmSecureHazardList> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.tcdmSecureHazardListService.queryById(id));
    }

    /**
     * 查询所有数据
     *
     * @return 所有数据
     */
    @GetMapping("findAll")
    public List<TcdmSecureHazardList> findAll() {
        return tcdmSecureHazardListService.findList();
    }

    /**
     * 新增数据
     *
     * @param tcdmSecureHazardList 实体
     * @return 新增结果
     */

    @PostMapping("add")
    public ResponseEntity<TcdmSecureHazardList> add(@RequestBody TcdmSecureHazardList tcdmSecureHazardList) {
        return ResponseEntity.ok(this.tcdmSecureHazardListService.insert(tcdmSecureHazardList));
    }

    /**
     * 编辑数据
     *
     * @param tcdmSecureHazardList 实体
     * @return 编辑结果
     */

    @PutMapping("put")
    public ResponseEntity<TcdmSecureHazardList> edit(@RequestBody TcdmSecureHazardList tcdmSecureHazardList) {
        return ResponseEntity.ok(this.tcdmSecureHazardListService.update(tcdmSecureHazardList));
    }

    /**
     * 删除数据
     *
     * @param tcdmSecureHazardList 主键
     * @return 删除是否成功
     */

    @DeleteMapping("del")
    public ResponseEntity<Boolean> deleteById(@RequestBody TcdmSecureHazardList tcdmSecureHazardList) {
        return ResponseEntity.ok(this.tcdmSecureHazardListService.deleteById1(tcdmSecureHazardList.getTshlId()));
    }


    /**
     * 通过条件查询获取统计数据
     * @param tcdmSecureHazardListDTO
     * @return
     */
    @PostMapping("/getTrend")
    public InventoryTrend getTrend(@RequestBody TcdmSecureHazardListDTO tcdmSecureHazardListDTO) throws ParseException {
        //首先判断条件查询中是否上传时间如果没有上传时间则查询当前日期的前七天中隐患数据
        LocalDate endTime=LocalDate.now();
        Date end=null;
        Date star=null;
        if(tcdmSecureHazardListDTO.getMyTime2().getEndTime()!=null&&tcdmSecureHazardListDTO.getMyTime2().getEndTime()!=""){
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            end=format.parse(tcdmSecureHazardListDTO.getMyTime2().getEndTime());
        }
        if(tcdmSecureHazardListDTO.getMyTime2().getStartTime()!=null&&tcdmSecureHazardListDTO.getMyTime2().getStartTime()!=""){
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            star =format.parse(tcdmSecureHazardListDTO.getMyTime2().getStartTime());

        }
        //查询当前条件下三种状态（已处理，处理中，挂起中）的数量
        return this.tcdmSecureHazardListService.getTrend(tcdmSecureHazardListDTO.getTcdmSecureHazardList(),star,end);
    }
}

